【问题标题】:Modifying the width of an underline using attributed text?使用属性文本修改下划线的宽度?
【发布时间】:2015-07-27 23:42:47
【问题描述】:

有没有办法修改NSAttributedString下划线的线宽?

看起来我可以很容易地修改颜色,但我不能轻易地修改下划线本身的宽度。

【问题讨论】:

  • 刚刚意识到 - 你是指线条的宽度还是粗细?如果它是宽度(比如它下划线的字母),则设置为 NSAttributedString 中的范围
  • 你找到解决办法了吗?
  • 之前的两个解决方案可以解决问题(带有特定宽度的下划线),但并不能真正解决问题。

标签: ios objective-c nsstring nsattributedstring


【解决方案1】:

您可以设置NSUnderlineStyleThickNSUnderlineStyleSingle,例如:

NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Thick underline" attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick)}];
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Normal underline" attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}];

下划线样式的完整列表在这里:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/NSAttributedString_UIKit_Additions/index.html#//apple_ref/c/tdef/NSUnderlineStyle

【讨论】:

    【解决方案2】:

    // 假设标签名称为“标签”

    //下划线代码

    CGSize expectedLabelSize = [@"Some text" sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:UILineBreakModeWordWrap];
    
    UIView *viewForUnderline=[[UIView alloc] init];
    viewForUnderline.frame=CGRectMake((label.frame.size.width - expectedLabelSize.width)/2,    expectedLabelSize.height + (label.frame.size.height - expectedLabelSize.height)/2,   expectedLabelSize.width, 1);
    viewForUnderline.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:viewForUnderline];
    

    或者您可以使用以下代码行

    label.attributedText = [[NSAttributedString alloc] initWithString:@"Some Text" 
                                                         attributes:underlineAttribute];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      相关资源
      最近更新 更多