Skip to content

xiangwangfeng/M80AttributedLabel

Repository files navigation

M80AttributedLabel

Another attributed label using CoreText 中文版本

Features

  • Multi-line Rich Text Display
  • Auto Link Detection and Custom Links Supported
  • Embedding Image and Custom Views
  • Following Properties Supported:
    • font
    • textColor
    • highlightColor
    • linkColor
    • underLineForLink
    • numbersOfLines (Truncates Last Line Supported)
    • textAlignment
    • lineBreakMode
    • lineSpacing
    • paragraphSpacing
    • shadowColor
    • shadowOffset
    • shadowBlur

System Requirement

  • iOS 7.0 or later
  • ARC required

Installation

Podfile

pod 'M80AttributedLabel'

Manually

  • git clone https://github.com/xiangwangfeng/M80AttributedLabel.git
  • Copy files in Class/M80AttributedLabel to your project
  • Add CoreText.framework to your target

Usage

Basic

M80AttributedLabel *label = [[M80AttributedLabel alloc]initWithFrame:CGRectZero];
    
label.text      = @"Hello M80AttributedLabel";
label.font      = [UIFont fontWithName:@"Zapfino" size:25];
label.textColor = M80RGB(0xFF9F00);
label.shadowColor = [UIColor grayColor];
label.shadowOffset= CGSizeMake(1, 1);
label.shadowBlur = 1;
    
label.frame     = CGRectInset(self.view.bounds,20,20);

[self.view addSubview:label];
   

Link

M80AttributedLabel *label = [[M80AttributedLabel alloc]initWithFrame:CGRectZero];
    
NSString *text  = @"The game which I current play is hearthstone,and its website is www.hearthstone.com.cn";
NSRange range   = [text rangeOfString:@"hearthstone"];
label.text      = text;
[label addCustomLink:[NSValue valueWithRange:range]
            forRange:range];
label.delegate = self;
    
label.frame     = CGRectInset(self.view.bounds,20,20);

[self.view addSubview:label];

Image

M80AttributedLabel *label = [[M80AttributedLabel alloc]initWithFrame:CGRectZero];
label.lineSpacing = 5.0;
    
[label appendImage:[UIImage imageNamed:@"avatar"] 
		    maxSize:CGSizeMake(40, 40)
       	  margin:UIEdgeInsetsZero
 	      alignment:M80ImageAlignmentBottom];
    
NSString *text  = @"say:\n有人问一位登山家为什么要去登山——谁都知道登山这件事既危险,又没什么实际的好处。[haha][haha][haha][haha]他回答道:“因为那座山峰在那里。”我喜欢这个答案,因为里面包含着幽默感——明明是自己想要登山,偏说是山在那里使他心里痒痒。除此之外,我还喜欢这位登山家干的事,没来由地往悬崖上爬。[haha][haha][haha]它会导致肌肉疼痛,还要冒摔出脑子的危险,所以一般人尽量避免爬山。[haha][haha][haha]用热力学的角度来看,这是个反熵的现象,所发趋害避利肯定反熵。";
    
//replace [haha] by image
NSArray *components = [text componentsSeparatedByString:@"[haha]"];
NSUInteger count = [components count];
for (NSUInteger i = 0; i < count; i++)
{
    [label appendText:[components objectAtIndex:i]];
    if (i != count - 1)
    {
        [label appendImage:[UIImage imageNamed:@"haha"]
                       maxSize:CGSizeMake(15, 15)
                        margin:UIEdgeInsetsZero
                     alignment:M80ImageAlignmentCenter];
    }
}
    
label.frame     = CGRectInset(self.view.bounds,20,20);
[self.view addSubview:label];
    
    

Attachemnt

M80AttributedLabel *label = [[M80AttributedLabel alloc]initWithFrame:CGRectZero];
    
for (NSInteger i = 0; i < 3; i++)
{
	[label appendText:@"Click the icon to fire event"];
	
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	button setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
	[button addTarget:self
              action:@selector(buttonClicked:)
    forControlEvents:UIControlEventTouchUpInside];
    [button setBounds:CGRectMake(0, 0, 30, 30)];
    [button setTag:i];
    [label appendView:button];
        
    [label appendText:@"\n\n\n"];
}
    

label.frame     = CGRectInset(self.view.bounds,20,20);
[self.view addSubview:label];

Contact

License

M80AttributedLabel is available under the MIT license. See the LICENSE file for more info.