M80AttributedLabel
Another attributed label using CoreText
Install / Use
/learn @xiangwangfeng/M80AttributedLabelREADME
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:
fonttextColorhighlightColorlinkColorunderLineForLinknumbersOfLines(Truncates Last Line Supported)textAlignmentlineBreakModelineSpacingparagraphSpacingshadowColorshadowOffsetshadowBlur
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/M80AttributedLabelto your project - Add
CoreText.frameworkto 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];
<img src="./Documents/Images/Basic.png" width="320">
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];
<img src="./Documents/Images/Link.png" width="320">
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];
<img src="./Documents/Images/Image.png" width="320">
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];
<img src="./Documents/Images/Attachment.png" width="320">
Contact
- https://github.com/xiangwangfeng
- xiangwangfeng@gmail.com
- http://weibo.com/epmao
License
M80AttributedLabel is available under the MIT license. See the LICENSE file for more info.
Related Skills
node-connect
347.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
