label富文本 以及图文混排

本文介绍iOS平台下富文本处理的方法,包括使用NSAttributedString设置文本样式、动态更新文本颜色实现淡入效果,以及借助GONMarkuoParser等开源库解析HTML标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//    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);   // An NSNumber containing an integer value.  0 means horizontal text.  1 indicates vertical text.  If not specified, it could follow higher-level vertical orientation settings.  Currently on iOS, it's always horizontal.  The behavior for any other value is undefined.

//    


    

    

    UILabel * label = [[UILabel alloc] init];

    label.bounds = CGRectMake(0, 0, 300, 300);

    label.center = self.view.center;

    


    

    

    

    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:@"this is attributeString testthis is attributeString testthis is attributeString testthis is attributeString testthis is attributeString testthis is attributeString testthis is attributeString test"];

    

    //设置字体

    [attributeStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, 5)];

    

    

    //背景颜色

    [attributeStr addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(5, 3)];

    

    

    //文字颜色

    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];

    

    

    //对某个 range进行 多个属性设置

    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:  NSUnderlineStyleSingle],NSUnderlineStyleAttributeName,[UIColor grayColor],NSForegroundColorAttributeName,nil];

    [attributeStr addAttributes:dic range:NSMakeRange(8, 10)];

    

    

    //对已经存在的属性进行移除

    [attributeStr removeAttribute:NSUnderlineStyleAttributeName range:NSMakeRange(8, 5)];

    

//    [attributeStr addAttributes: range:<#(NSRange)#>];

    

    

    //设置段落格式

    NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];

    

    style.lineSpacing = 10.f;

    style.paragraphSpacing = 10.f;

    

    [attributeStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0,attributeStr.length)];

    

    

    label.numberOfLines = 0;

    

    label.attributedText = attributeStr;

    

    [self.view addSubview:label];

    

    

富文本使用事例: label中文字的淡入, 每个文字淡入的速度不同

- (void) update:(id) sender

{

    //先计算出一个算计的alpha数组

    

    BOOL  shouldRemoveDisplay = YES;

    [_attributeString removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, _length)];

    for ( int i=0 ; i<_length; i++)

    {

        

        float alpha = [[_alphaArray objectAtIndex:i] floatValue];

        alpha = MAX( MIN(1, alpha) , 0);

        if ( alpha < 1 )

        {

            shouldRemoveDisplay = NO;

            

        }

        

        

        [_attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:1 green:0 blue:0 alpha:alpha] range:NSMakeRange(i, 1)];

        

    }

    

    NSMutableArray * array = [[NSMutableArray alloc] init];

    for ( int i=0 ; i<_length; i++)

    {

        

        float alpha = [[_alphaArray objectAtIndex:i] floatValue];

        if ( alpha < 1 ) {

            

            alpha += 1.0/40;

        }

   

        [array  addObject:@(alpha)];

    }

    _alphaArray = array;

    

    

     _label.attributedText = _attributeString;

    

    if( shouldRemoveDisplay )

    {

        [sender removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

        sender = nil;

    }

    

    

}



- (void) alphaArray1111

{

    _alphaArray = [[NSMutableArray alloc] init];

    for ( int i=0 ; i<_length; i++)

    {

    

        int count = arc4random()%_length;

        CGFloat alpha = count/(CGFloat) _length;

        [_alphaArray addObject:@(alpha)];


    }

    

    return;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    

    CADisplayLink * displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update:)]; // 1/60就运行一次

    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];//分片

    

    

    self.view.backgroundColor = [UIColor yellowColor];

    _label = [[UILabel alloc] init];

    _label.frame = self.view.bounds;

    

   _attributeString =  [[NSMutableAttributedString alloc] initWithString: @"年年社日停针线。\n怎忍见、双飞燕。\n今日江城春已半。\n一身犹在,乱山深处,寂寞溪桥畔。\n春衫著破谁针线。\n点点行行泪痕满。\n落日解鞍芳草岸。\n花无人戴,酒无人劝,醉也无人管\n"];


    

    for ( int i=0 ; i<_length; i++)

    {

        

        float alpha = [[_alphaArray objectAtIndex:i] floatValue];

        

        [_attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:1 green:0 blue:0 alpha:0] range:NSMakeRange(i, 1)];

        

    }

    _length = [_attributeString length];

    

    [self alphaArray1111];

    

    

    _label.numberOfLines = 0;

    _label.textAlignment = 1;

    

    [self.view addSubview:_label];

    

    

    

}









    

    

    //2  GONMarkuoParser

    /*

     

     富文本处理方式 2  开源库GONMarkuoParser处理富文本

     

     1。导入GonMarkupParser开源库

        import "GONMarkupParser_All.h"

     

     2. 使用html类似标签完成富文本

     */

    

    

    

    

    //3.  IOS7发布  UITextKit文本处理框架

    //UITextKit:用于图文混排

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值