当前位置: 首页 > 知识库问答 >
问题:

ios - iOS 开发:UIButton 使用 AttributedString 显示图像和文字失败问题?

芮学
2024-10-17
- (UIButton*)searchBarButton{
    if (_searchBarButton == nil) {
        _searchBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchBarButton.backgroundColor = [UIColor whiteColor];
        _searchBarButton.layer.cornerRadius = 8;
        _searchBarButton.layer.masksToBounds = YES;
        /*! 字体其他 */
        NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:@"A/B/C"];
        attributedText.yy_alignment = NSTextAlignmentLeft;
        attributedText.yy_font = [UIFont fontWithName:DSLFONT size:13];
        attributedText.yy_color = [UIColor grayColor];
        NSMutableAttributedString *attachment = nil;
        UIFont *font = [UIFont fontWithName:DSLFONT size:13];
        UIImageView*playVice = [UIImageView new];
        playVice.image = [UIImage imageNamed:@"MianSearch"];
        playVice.size = CGSizeMake(10, 10);
        attachment = [NSMutableAttributedString yy_attachmentStringWithContent:playVice contentMode:UIViewContentModeScaleAspectFill attachmentSize:playVice.size alignToFont:font alignment:YYTextVerticalAlignmentTop];
        [attributedText insertAttributedString:attachment atIndex:0];
        [_searchBarButton setAttributedTitle:attributedText forState:UIControlStateNormal];
        [_searchBarButton addTarget:self action:@selector(searchBarButtonButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBarButton;
}

我想实现这种效果,image.png
但是以上代码无法实现,但是我没有找到问题所在,恳请帮助,非常感谢。

使用以上代码,多次尝试无效果。非常感谢。

共有1个答案

水昊阳
2024-10-17

在你的代码中,有几个问题和潜在的误解需要解决。首先,yy_attachmentStringWithContent 和相关的 yy_ 方法(如 yy_alignment, yy_font, yy_color)并不是 NSMutableAttributedString 或 UIKit 标准库中的方法。这些方法看起来像是来自某个第三方库(如 YYText),但如果你没有正确导入和配置这个库,这些方法将不会工作。

此外,UIImageViewsize 属性并不是直接设置的,你需要通过 framebounds 来调整其大小。但在这个上下文中,你通常不需要直接操作 UIImageView 的大小,因为 attachmentSize 参数在创建附件时应该已经指定了大小。

下面是一个使用标准 UIKit 方法(不使用 YYText 或其他第三方库)来实现你的需求的示例代码。如果你确实需要使用 YYText,你需要确保已经正确安装并导入了该库。

使用 UIKit 标准方法

如果你不使用 YYText,你可以通过创建一个自定义的 UIView 来包含 UIImageViewUILabel,然后将这个自定义视图作为按钮的背景视图或内容视图。但这里,为了简化,我将展示如何使用 UIButtonsetImagesetTitle 方法来分别设置图像和文本(虽然这不会直接支持在同一行中混合文本和图像)。

如果你确实需要混合文本和图像,并且希望使用 NSAttributedString,你可能需要寻找或实现一个自定义的 UIButton 子类或使用像 YYText 这样的库。

示例代码(不使用 NSAttributedString 混合图像和文本)

- (UIButton*)searchBarButton {
    if (_searchBarButton == nil) {
        _searchBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchBarButton.backgroundColor = [UIColor whiteColor];
        _searchBarButton.layer.cornerRadius = 8;
        _searchBarButton.layer.masksToBounds = YES;

        // 设置图像
        [_searchBarButton setImage:[UIImage imageNamed:@"MianSearch"] forState:UIControlStateNormal];
        [_searchBarButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)]; // 根据需要调整图像的内边距

        // 设置标题
        [_searchBarButton setTitle:@"A/B/C" forState:UIControlStateNormal];
        [_searchBarButton.titleLabel setFont:[UIFont fontWithName:DSLFONT size:13]];
        [_searchBarButton.titleLabel setTextColor:[UIColor grayColor]];

        // 添加点击事件
        [_searchBarButton addTarget:self action:@selector(searchBarButtonButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBarButton;
}

如果你确实需要更复杂的布局(如文本和图像在同一行),你可能需要查看像 YYText 这样的库,或者自己实现一个自定义的 UIButton 子类。

 类似资料:
  • 问题内容: 我目前正在使用iOS应用程序,没什么大不了的,只是一个简单的预算跟踪程序。工作流程如下:将一些新内容编码到应用程序中=>将我的iPhone连接到我的mac =>使用Xcode构建应用程序到我的iPhone 6和我妻子的iPhone 6 plus(所以有两个不同的设备!)。至此,一切正常。 但是过了一会儿(有时只有几天,有时是几周),该应用程序突然同时停止在两个设备上运行,而没有任何前兆

  • https://github.com/ChenYilong/iOSInterviewQuestions http://draveness.me/guan-yu-xie-ios-wen-ti-de-jie-da/ https://github.com/lzyy/iOS-Developer-Interview-Questions

  • 在我的应用程序中包含CN1Bluetooth代码后,我收到了一个构建失败。它在lib文件夹中,已经刷新了。在Android上工作,不会为iOS编译。 CompileC/var/folders/p7/d3z112yd0156kxkm2p21p8ym0000gn/t/build6615573583221835807xx/dist/build/build/mederationates/pods.buil

  • iOS 工程师技能树 http://segmentfault.com/a/1190000002946644 Objective-C Objective-C语言基础 library,framework的制作 Runtime 编程 LLVM 原理和调优 操作系统 iOS内存管理和调优 iOS的文件系统和沙盒机制 iOS多线程编程(Thread,GCD,NSOperation) iOS网络和服务器编程(

  • 我得到这个消息从终端时运行我的项目与命令。我不得不说在今天之前一切都很好,Xcode从我的苹果电脑上更新了。 我什么都试过了,但没有成功。需要帮忙吗? 以下生成命令失败: Ld/Users/RobertoDiCarrera/documents/wander/wanderapp/Platform/ios/build/emulator/HelloWorld。app/HelloWorld normal

  • 我遇到一个问题,当我尝试显示一个图像后,我点击一个按钮并选择图像文件在“选择文件对话框”。 最初,我设法在中显示选定的图像,但后来我创建了一个单独的,我想从那以后就开始出错了。无论我选择什么图像,都不会显示它。 我调试了它,并确保文件选择器确实将图像传递给,确实从获得值,但即使在和之后,它也不显示图像。 在这里,我附上我的代码,供您友好的参考! (我对代码进行了修剪,以保持干净的外观,因此可能会留

  • 如果你已经踏入原生iOS app开发之中,你可以跳过这一节去看Core Animation入门。但如果你刚开始iOS app的开发,抓住其基本就很重要,这样你就可以完全理解事物工作的原理。 Xcode Xcode是Mac和iOS开发者用来创建原生应用的IDE(集成开发环境)。它不仅仅是一个像你可能用过的Sublime Text之类的代码编辑器,它是设计、编程、测试、调试和分发Mac和iOS应用的整

  • 本文向大家介绍iOS 11 AppIcon不显示问题小结,包括了iOS 11 AppIcon不显示问题小结的使用技巧和注意事项,需要的朋友参考一下 今天更新Xcode 9 后,在运行老项目时遇到一个小坑,就是无论如何都不显示AppIcon,在网络上找到了方法,单并没有解决,其实不是方法的问题,只是有一个小细节要注意,在这里提示一下. 出现这个问题的原因就是cocoapods与iOS 11出现点问题