iOS富文本執行個體

來源:互聯網
上載者:User

標籤:技術   view   control   .text   port   location   http   import   gre   

:

 

代碼:

#import "ViewController.h"

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 50)];

    lab.numberOfLines = 0;

    lab.text = @"朦朦朧朧萌萌噠:忽地薩發羅夫的送回國的薩可減肥經典款撒積分兌換可舒服了地方上課啦放假啊大家思考理髮店說的就是咖喱驚魂甫定了件快樂的撒進防空洞酸辣粉瞭解到撒度撒化肥丹江口市啦大家看撒的環境撒發了大家看撒發的撒了咖啡店撒克拉的第撒對空射擊啊。";

    lab.font = [UIFont systemFontOfSize:10];

    NSRange range = [lab.text rangeOfString:@":"];

    [self.view addSubview:lab];

    

    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:lab.text];

    [att addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}  range:NSMakeRange(0, range.location+1)];

    lab.attributedText = att;

    

    UILabel *lab1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 300, 20)];

    lab1.text = @"蹦蹦跳跳正在遠離回複星星點燈追逐:好傻啊你";

    lab1.font = [UIFont systemFontOfSize:10];

    [self.view addSubview:lab1];

    

    NSRange range1 = [lab1.text rangeOfString:@"回複"];

    NSRange range2 = [lab1.text rangeOfString:@":"];

    NSMutableAttributedString *att1 = [[NSMutableAttributedString alloc] initWithString:lab1.text];

    [att1 addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}  range:NSMakeRange(0, range1.location)];

    [att1 addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(range1.location+2, range2.location-range1.location-1)];

    NSLog(@"%d  %d",range1.location,range2.location);

    lab1.attributedText = att1;

}

@end

 

iOS富文本執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.