iOS之UILabel和UITextView富文字操作
阿新 • • 發佈:2019-02-12
//
// ViewController.m
// NSMutableAttributedString
//
// Created by WangLe on 16/5/23.
// Copyright © 2016年 WangLe. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITextViewDelegate>
@property (nonatomic, strong) UITextView * textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 50, 200, 200)];
UILabel * myLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 300, 200, 200)];
myLabel.backgroundColor = [UIColor yellowColor];
_textView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:myLabel];
[self.view addSubview:_textView];
// 定義一個可變屬性字串物件
NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:@"緩緩飄落的楓葉像思念我點燃燭火溫暖歲末的秋天鐳射掠過天邊被風掠過想你的思念"];
// 設定字型大小 range是設定範圍,下同
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 5)];
// 設定字型顏色
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(2, 5)];
// 設定下劃線
[str addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(3, 7)];
// 設定字型樣式
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Geeza Pro" size:25] range:NSMakeRange(5, 5)];
//NSLog(@"字型集合%@",[UIFont familyNames]);
// 刪除線 常用於劃掉原價
[str addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(8, 5)];
// 刪除線的顏色(先設定刪除線再設定顏色)
[str addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, 5)];
// 設定空心字
[str addAttribute:NSStrokeWidthAttributeName value:@1 range:NSMakeRange(18, 5)];
// 插入圖片
NSTextAttachment * att = [[NSTextAttachment alloc]init];
att.image = [UIImage imageNamed:@"2"];
NSAttributedString * attStr = [NSAttributedString attributedStringWithAttachment:att];
[str insertAttributedString:attStr atIndex:25];
// 新增連結
[str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.baidu.com"] range:NSMakeRange(30, 6)];
// 建立字型段落 行間距 格式
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 50;
paragraphStyle.firstLineHeadIndent = 30;// 設定為字型大小大兩倍
// _textView.attributedText = [[NSAttributedString alloc] initWithString:str.string attributes:@{
// NSFontAttributeName:[UIFont systemFontOfSize:15],
// NSParagraphStyleAttributeName:paragraphStyle
// }];
[str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.string.length)];
// 這句不能寫前面,不然沒效果
_textView.attributedText = [str copy];
_textView.editable = NO;
_textView.delegate = self;
myLabel.attributedText = str;
myLabel.numberOfLines = 0;
}
/**
* 點選圖片觸發代理事件
*/
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
{
NSLog(@"圖片%@", textAttachment);
return NO;
}
/**
* 點選連結,觸發代理事件
*/
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
[[UIApplication sharedApplication] openURL:URL];
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果圖如下
05232225.png