今天主要寫(xiě)兩個(gè)東西
<1> 一個(gè)是左面是用戶(hù)頭像或小標(biāo)志, 右面是輸入框, 這樣情況分開(kāi)寫(xiě)雖然不是很麻煩, 但是為了讓代碼顯得高端大氣上檔次, 我們通常要封裝一個(gè)view類(lèi).
/* 注意:我是在MRC下寫(xiě)的*/
自定義View的.h文件
#import <UIKit/UIKit.h>
@interface MyView : UIView
// 這個(gè)是自定義View的左邊imageView屬性
@property (nullable, nonatomic, retain) UIImageView *imageViewLeft;
// 這是自定義View的右面的textfield屬性
@property (nullable, nonatomic, retain) UITextField *textFieldRight;
// 這個(gè)是左邊imageView和右邊textfield的frame的寬之比(這個(gè)值在零到一之間)
@property (nonatomic, assign) CGFloat ratio;
// 初始化 這里傳進(jìn)來(lái)的參數(shù):frame是外界給的自定義View的frame ratio是寬之比 image是左邊ImageView的image placeHolder是輸入框的占位符
- (nullable instancetype)initWithFrame:(CGRect)frame ratio:(CGFloat)ratio withImage:(nullable UIImage *)image placeHolder:(nullable NSString *)placeHolder;
@end
自定義View的.m文件
#import "MyView.h"
@implementation MyView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)dealloc {
[_imageViewLeft release];
[_textFieldRight release];
[super dealloc];
}
// 通用
- (instancetype)initWithFrame:(CGRect)frame {
return [self initWithFrame:frame ratio:0 withImage:nil placeHolder:nil];
}
// 指派初始化
- (instancetype)initWithFrame:(CGRect)frame ratio:(CGFloat)ratio withImage:(UIImage *)image placeHolder:(NSString *)placeHolder {
self = [super initWithFrame:frame];
if (self) {
// 創(chuàng)建視圖對(duì)象
[self createSubViewsWithFrame:frame ratio:ratio];
self.imageViewLeft.image = image;
self.textFieldRight.placeholder = placeHolder;
}
return self;
}
- (void)createSubViewsWithFrame:(CGRect)frame ratio:(CGFloat)ratio {
self.imageViewLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width * ratio, frame.size.height)];
[self addSubview:_imageViewLeft];
[_imageViewLeft release];
self.textFieldRight = [[UITextField alloc] initWithFrame:CGRectMake(frame.size.width * ratio, 0, frame.size.width * (1 - ratio), frame.size.height)];
[self addSubview:_textFieldRight];
[_textFieldRight release];
}
@end
然后導(dǎo)入視圖控制器里用就可以用了
效果圖如下:
屏幕快照 2016-07-16 下午4.49.52.png
<2> 另一個(gè)是文本上的點(diǎn)擊超鏈接
這塊也是比較方便的在使用點(diǎn)擊文本跳轉(zhuǎn)的時(shí)候
這里主要用到TTTAttributeLabel(可以在github上下載)
由于篇幅較長(zhǎng)這里就不解釋了, 感興趣的可以自己研究研究.
這里直接在VC中導(dǎo)入, 引用
#import "ViewController.h"
#import "WH_AttributedLabel.h"
@interface ViewController () <WH_AttributedLabelDelegate, UIAlertViewDelegate>
@end
@implementation ViewController
static inline NSRegularExpression *NameRegularExpression(){
static NSRegularExpression *_nameRegularExpression = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_nameRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"^\\w+" options:NSRegularExpressionCaseInsensitive error:nil];
});
return _nameRegularExpression;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
WH_AttributedLabel *label = [[WH_AttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
label.center = self.view.center;
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor blackColor];
label.lineBreakMode = NSLineBreakByCharWrapping;
label.numberOfLines = 0;
// 設(shè)置高亮顏色
label.highlightedTextColor = [UIColor greenColor];
// 檢測(cè)url
label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
// 對(duì)齊方式
label.verticalAlignment = WH_AttributedLabelVerticalAlignmentCenter;
// 行間距
label.lineSpacing = 8;
// 設(shè)置陰影
// label.shadowColor = [UIColor grayColor];
// delegate
label.delegate = self;
// NO 不顯示下劃線(xiàn)
label.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
NSString *text = @"有問(wèn)題, 點(diǎn)這里";
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
// 設(shè)置可點(diǎn)文字的范圍
NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"點(diǎn)這里" options:NSCaseInsensitiveSearch];
// 設(shè)定可點(diǎn)擊文字的大小
UIFont *boldSystwmFont = [UIFont boldSystemFontOfSize:14];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystwmFont.fontName, boldSystwmFont.pointSize, NULL);
if (font) {
// 設(shè)置可點(diǎn)擊文本的大小
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
// 設(shè)置可點(diǎn)擊文本的顏色
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:boldRange];
CFRelease(font);
}
return mutableAttributedString;
}];
NSRegularExpression *regexp = NameRegularExpression();
NSRange linkRange = [regexp rangeOfFirstMatchInString:text options:0 range:NSMakeRange(5, 3)];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:www.baidu.com"]];
// 設(shè)置連接的url
[label addLinkToURL:url withRange:linkRange];
[self.view addSubview:label];
}
- (void)attributedLabel:(__unused WH_AttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
[[UIApplication sharedApplication] openURL:url];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果如下:
文本.gif