一句話筆記着饥,某段時(shí)間內(nèi)遇到或看到的某個(gè)可記錄的點(diǎn)。 2016-8-6
- 1、修改button 點(diǎn)中后的高亮狀態(tài)的顏色
- 2、KVC 中 forKey,forKeyPath的區(qū)別
- 3呵恢、Category 不需要引用了,可以直接使用
- 4媚创、UILabel 中文字居右時(shí)的留空格
1渗钉、修改button 點(diǎn)中后的高亮狀態(tài)的顏色
說明一下,此處不是直接修改背景顏色, 下面這個(gè)是網(wǎng)上流傳的方法钞钙,不過有時(shí)是有用的鳄橘,但此處不是我的需求。
- (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)然實(shí)際上挥唠,我這邊這個(gè)需求的理解也可以直接將點(diǎn)中后的高亮狀態(tài)的顏色變?yōu)闆]有:
[button setAdjustsImageWhenHighlighted:NO];
具體的還是看需求吧。
2焕议、KVC 中 forKey,forKeyPath的區(qū)別
一般的修改一個(gè)對(duì)象的屬性的時(shí)候,forKey和forKeyPath, 沒什么區(qū)別弧关。但是forKeyPath中可以利用運(yùn)算符, 可以一層一層往下查找對(duì)象的屬性 盅安,能夠找到更深層的屬性。所以用forKeyPath就行了世囊,因?yàn)樗_實(shí)更強(qiáng)大一些别瞭。
3、Category 不需要引用了株憾,可以直接使用
以前我們要使用Category 必須要頭文件引入一下蝙寨,現(xiàn)在當(dāng)我們創(chuàng)建了之后晒衩,它相當(dāng)于放到我們之前的那個(gè)pch文件中,也就是全局引用了墙歪。但是往往有時(shí)候里面我們沒有寫好的情況下听系,一不注意就出錯(cuò)了,一下子不知道錯(cuò)誤在哪里虹菲,所以還是要注意一下的靠胜。
4、UILabel 中文字居右時(shí)的留空格
通常我們用UILabel 的時(shí)候毕源,會(huì)有前面留空格或后面留空格
label.text = [NSString stringWithFormat:@"%@%@",@" ",@"strOne"];
label.textAlignment = NSTextAlignmentLeft;
但是右邊這樣是是不管用的
label.text = [NSString stringWithFormat:@"%@%@",@"strOne",@" "];
label.textAlignment = NSTextAlignmentRight;
用 \t
就好啦
label.text = [NSString stringWithFormat:@"%@%@",@"strOne",@"\t"];
但是那個(gè)邊距也不是能絕對(duì)控制距離的浪漠,要空遠(yuǎn)一點(diǎn)我們可以多加一個(gè)\t
, 但\t
在不同型號(hào)的手機(jī)中的顯示那個(gè)距離是不一樣的, 所以要精準(zhǔn),還是得用其他辦法霎褐,自定義之類的址愿。
常用的方法就是直接繼承UILabel, 增加一個(gè)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)邊距所寫的一個(gè)繼承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啦歌粥。