UIButton 上的 iOS NSAttributedString

标签 ios uibutton nsattributedstring

我使用的是 iOS 6,所以属性字符串应该很容易使用,对吧?好吧……没那么多。

我想做的事情:

使用 UIButton 的自定义子类(它不对 titleLabel 做任何自定义),我想要一个多行的属性标题:

  1. 第一行全部大写(我知道这不是属性的一部分)
  2. 粗体在第一行
  3. 在第一行加下划线
  4. 第二行的“正常”重量
  5. 第二行没有下划线
  6. 以两条线为中心

到目前为止,我已经能够获得 # 的 1 到 5(至少,我认为我做到了,但当前的测试在多行文本中产生错误),但是当我尝试做某事时(任何事情! ) 使文本居中,我的应用程序不断崩溃。当我尝试让所有 6 个项目正常工作(通过各种方法)时,出现以下崩溃/错误:

Terminating app due to uncaught exception 
'NSInternalInconsistencyException', reason: 
'NSAttributedString invalid for autoresizing, 
it must have a single spanning paragraph style
(or none) with a non-wrapping lineBreakMode.'

根据我的尝试,我似乎可以选择以下选项之一,但不能同时选择两者:

  1. 多行居中标签
  2. 属性标签

如果我必须,我可以选择其中之一,但我不敢相信我不能拥有一个看似相当简单的概念。

有人可以告诉我我做错了什么吗?

这是我正在尝试的代码的最后一次迭代:

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];

UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue-Medium" size:20.0f];
UIFont *font2 = [UIFont fontWithName:@"HelveticaNeue-Light"  size:20.0f];
NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),  
                        NSFontAttributeName:font1};
NSDictionary *dict2 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),    
                        NSFontAttributeName:font2};

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"LINE 1\n"    attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"line 2"      attributes:dict2]];
[[self buttonToStyle] setAttributedTitle:attString forState:UIControlStateNormal];
[[[self buttonToStyle] titleLabel] setNumberOfLines:0];
[[[self buttonToStyle] titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];

最佳答案

在我看来,您好像忘记在代码中使用您设置的“样式”对象……您只是将其实例化。您应该将代码修改为如下所示:

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];

UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue-Medium" size:20.0f];
UIFont *font2 = [UIFont fontWithName:@"HelveticaNeue-Light"  size:20.0f];
NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
                        NSFontAttributeName:font1,
                        NSParagraphStyleAttributeName:style}; // Added line
NSDictionary *dict2 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
                        NSFontAttributeName:font2,
                        NSParagraphStyleAttributeName:style}; // Added line

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"LINE 1\n"    attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"line 2"      attributes:dict2]];
[self.resolveButton setAttributedTitle:attString forState:UIControlStateNormal];
[[self.resolveButton titleLabel] setNumberOfLines:0];
[[self.resolveButton titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];

请注意,我只添加了定义 NSParagraphStyleAttributeName 的行。其他一切都是一样的。这就是我得到的按钮:

enter image description here

这是在 Swift 3.0 中

let style = NSMutableParagraphStyle()
style.alignment = .center
style.lineBreakMode = .byWordWrapping

guard
    let font1 = UIFont(name: "HelveticaNeue-Medium", size: 20),
    let font2 = UIFont(name: "HelveticaNeue-Light", size: 20)  else { return }

let dict1:[String:Any] = [
    NSUnderlineStyleAttributeName:NSUnderlineStyle.styleSingle.rawValue,
    NSFontAttributeName:font1,
    NSParagraphStyleAttributeName:style
]

let dict2:[String:Any] = [
    NSUnderlineStyleAttributeName:NSUnderlineStyle.styleNone.rawValue,
    NSFontAttributeName:font2,
    NSParagraphStyleAttributeName:style
]

let attString = NSMutableAttributedString()
attString.append(NSAttributedString(string: "LINE 1", attributes: dict1))
attString.append(NSAttributedString(string: "line 2", attributes: dict2))

button.setAttributedTitle(attString, for: .normal)
button.titleLabel?.numberOfLines = 0
button.titleLabel?.lineBreakMode = .byWordWrapping

关于UIButton 上的 iOS NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756067/

相关文章:

ios - Swift:如何根据按钮点击将结果显示到标签

objective-c - 在 UINavigationBar 中 setBackgroundImage 之后使用 barStyles

ios - 使用代码调整 UIButton 高度?

ios - 如何找回 iOS 7 中的 UIButton 边框?

iphone - UITableViewCell 子类不工作

iphone - 模拟器中的核心位置问题

swift - 如何在按钮上显示图像名称?我们什么时候上传?

html - 将嵌套的粗体斜体 HTML 标签转换为 NSAttributedString

ios - 如何修复 UITextView (iOS) 中修改的段落样式中的垂直选择 handle ?

ios - 控制台打印不同的文本字体 swift