一句話筆記,某段時間內(nèi)遇到或看到的某個可記錄的點。 2016-8-6
- 1、修改button 點中后的高亮狀態(tài)的顏色
- 2、KVC 中 forKey,forKeyPath的區(qū)別
- 3、Category 不需要引用了,可以直接使用
- 4、UILabel 中文字居右時的留空格
1、修改button 點中后的高亮狀態(tài)的顏色
說明一下,此處不是直接修改背景顏色, 下面這個是網(wǎng)上流傳的方法,不過有時是有用的,但此處不是我的需求。
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(60, 100, 200, 40)];
[button setTitle:@"button" forState:UIControlStateNormal];
[button setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateNormal];
[button setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted];
[self.view addSubview:button];
}
// 顏色轉(zhuǎn)換為背景圖片
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
經(jīng)過后面我的需求,唯有通過增加圖片才得以解決:
[button setImage:[UIImage imageNamed:@"normal_pic"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"highlighted_pic"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"selected_pic"] forState:UIControlStateSelected];
當(dāng)然實際上,我這邊這個需求的理解也可以直接將點中后的高亮狀態(tài)的顏色變?yōu)闆]有:
[button setAdjustsImageWhenHighlighted:NO];
具體的還是看需求吧。
2、KVC 中 forKey,forKeyPath的區(qū)別
一般的修改一個對象的屬性的時候,forKey和forKeyPath, 沒什么區(qū)別。但是forKeyPath中可以利用運算符, 可以一層一層往下查找對象的屬性 ,能夠找到更深層的屬性。所以用forKeyPath就行了,因為它確實更強(qiáng)大一些。
3、Category 不需要引用了,可以直接使用
以前我們要使用Category 必須要頭文件引入一下,現(xiàn)在當(dāng)我們創(chuàng)建了之后,它相當(dāng)于放到我們之前的那個pch文件中,也就是全局引用了。但是往往有時候里面我們沒有寫好的情況下,一不注意就出錯了,一下子不知道錯誤在哪里,所以還是要注意一下的。
4、UILabel 中文字居右時的留空格
通常我們用UILabel 的時候,會有前面留空格或后面留空格
label.text = [NSString stringWithFormat:@"%@%@",@" ",@"strOne"];
label.textAlignment = NSTextAlignmentLeft;
但是右邊這樣是是不管用的
label.text = [NSString stringWithFormat:@"%@%@",@"strOne",@" "];
label.textAlignment = NSTextAlignmentRight;

用 \t就好啦
label.text = [NSString stringWithFormat:@"%@%@",@"strOne",@"\t"];

但是那個邊距也不是能絕對控制距離的,要空遠(yuǎn)一點我們可以多加一個\t, 但\t在不同型號的手機(jī)中的顯示那個距離是不一樣的, 所以要精準(zhǔn),還是得用其他辦法,自定義之類的。
常用的方法就是直接繼承UILabel, 增加一個edgeInsets 屬性,重寫
// override points. can adjust rect before calling super.
// label has default content mode of UIViewContentModeRedraw
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines;
- (void)drawTextInRect:(CGRect)rect;
然后設(shè)置。下面是模仿iOS-設(shè)置UILabel的內(nèi)邊距所寫的一個繼承UILabel:
#import <UIKit/UIKit.h>
@interface YSLabel : UILabel
@property (nonatomic, assign) UIEdgeInsets edgeInsets;
@end
#import "YSLabel.h"
@implementation YSLabel
- (instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
_edgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
_edgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
CGRect rect = [super textRectForBounds:UIEdgeInsetsInsetRect(bounds,
_edgeInsets) limitedToNumberOfLines:numberOfLines];
//根據(jù)edgeInsets,修改繪制文字的bounds
rect.origin.x -= _edgeInsets.left;
rect.origin.y -= _edgeInsets.top;
rect.size.width += _edgeInsets.left + _edgeInsets.right;
rect.size.height += _edgeInsets.top + _edgeInsets.bottom;
return rect;
}
//繪制文字
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, _edgeInsets)];
}
@end
當(dāng)然此處我是先寫死設(shè)置靠右距離20的。如果需要額外設(shè)置,我們直接在外部設(shè)置edgeInsets就OK啦。