美文网首页iOS开发杂谈iOS开发心得
UIButton和UILabel添加下划线,中划线

UIButton和UILabel添加下划线,中划线

作者: 我的天空蔚蓝色 | 来源:发表于2016-01-08 15:30 被阅读4024次

    UIButton

    UIButton *butn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"我是Button"];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    [butn setAttributedTitle:str forState:UIControlStateNormal];//这个状态要加上
    [self.view addSubview:butn];
    

    UILabel

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 300, 40)];
    label.backgroundColor = [UIColor yellowColor];
    [label setFont:[UIFont systemFontOfSize:12]];
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"我是小白但我乐于分享,这是我的群246807516"]];
    NSRange strRange1 = {0,[str1 length]};
    [str1 addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange1];  label.attributedText = str1;
    [self.view addSubview:label];
    

    NSStrikethroughStyleAttributeName加中划线
    NSUnderlineStyleAttributeName加下划线

    /********** Attributes 这里总有你想要的***********/

    UIKIT_EXTERN NSString * const NSFontAttributeName NS_AVAILABLE(10_0, 6_0);                // UIFont, default Helvetica(Neue) 12
    UIKIT_EXTERN NSString * const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0);      // NSParagraphStyle, default defaultParagraphStyle
    UIKIT_EXTERN NSString * const NSForegroundColorAttributeName NS_AVAILABLE(10_0, 6_0);     // UIColor, default blackColor
    UIKIT_EXTERN NSString * const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0);     // UIColor, default nil: no background
    UIKIT_EXTERN NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0);            // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
    UIKIT_EXTERN NSString * const NSKernAttributeName NS_AVAILABLE(10_0, 6_0);                // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
    UIKIT_EXTERN NSString * const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0);  // NSNumber containing integer, default 0: no strikethrough
    UIKIT_EXTERN NSString * const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0);      // NSNumber containing integer, default 0: no underline
    UIKIT_EXTERN NSString * const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0);         // UIColor, default nil: same as foreground color
    UIKIT_EXTERN NSString * const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 6_0);         // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
    UIKIT_EXTERN NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0);              // NSShadow, default nil: no shadow
    UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE(10_10, 7_0);          // NSString, default nil: no text effect
    
    UIKIT_EXTERN NSString * const NSAttachmentAttributeName NS_AVAILABLE(10_0, 7_0);          // NSTextAttachment, default nil
    UIKIT_EXTERN NSString * const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0);                // NSURL (preferred) or NSString
    UIKIT_EXTERN NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0);      // NSNumber containing floating point value, in points; offset from baseline, default 0
    UIKIT_EXTERN NSString * const NSUnderlineColorAttributeName NS_AVAILABLE(10_0, 7_0);      // UIColor, default nil: same as foreground color
    UIKIT_EXTERN NSString * const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0);  // UIColor, default nil: same as foreground color
    UIKIT_EXTERN NSString * const NSObliquenessAttributeName NS_AVAILABLE(10_0, 7_0);         // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
    UIKIT_EXTERN NSString * const NSExpansionAttributeName NS_AVAILABLE(10_0, 7_0);           // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion
    
    UIKIT_EXTERN NSString * const NSWritingDirectionAttributeName NS_AVAILABLE(10_6, 7_0);    // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters.  The control characters can be obtained by masking NSWritingDirection and NSTextWritingDirection values.  LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,
    
    UIKIT_EXTERN NSString * const NSVerticalGlyphFormAttributeName NS_AVAILABLE(10_7, 6_0);  
    
    屏幕快照 2016-01-08 下午3.30.55.png

    相关文章

      网友评论

      • Jenny_e668:请问,下划线可以自己写宽度吗?
        我的天空蔚蓝色:@Jenny_e668 线的粗细跟字体有关系,要么就自己加个view上去
        我的天空蔚蓝色:@Jenny_e668 线的出息跟字体有关系,你可以不用富文本的方法,自己写个view盖上去
      • Tonlin:我来给你鼓励,写的真好
      • wahyx98:不错。
      • 8ae158dda3f2:UIKIT_EXTERN 和 extern 区别在哪里?
        莹bei___㤫:UIKIT下的封装 => UIKIT_EXTERN 原生extern 简而言之UIKIT_EXTERN 是 extern 的封装 方便使用而已,跟enum 和NS_ENUM 一样
      • 爃少:那个叫删除线。。

      本文标题:UIButton和UILabel添加下划线,中划线

      本文链接:https://www.haomeiwen.com/subject/spnfkttx.html