iOS UISearchBar 設(shè)置圓角以及設(shè)置placeholder的文字大小和顏色

項(xiàng)目中幾乎肯定是要用搜索這項(xiàng)功能的,但是關(guān)于搜索框的圓角問(wèn)題一直在困擾我!所以好好找了一下解決辦法,上干貨!

1.設(shè)置UISearchBar圓角

首先寫(xiě)一個(gè)UIImage的分類(lèi)

#import <UIKit/UIKit.h>

@interface UIImage (XYColor)

/** 返回一個(gè)傳入顏色值的iamge對(duì)象 */
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size;

@end
#import "UIImage+XYColor.h"

@implementation UIImage (XYColor)

/** 返回一個(gè)傳入顏色值的iamge對(duì)象 */
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end

用的時(shí)候如下

searchBar.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(SCREEN_WIDTH - 80*WidthRatio, 30*WidthRatio)];
searchBar.layer.masksToBounds = YES;
searchBar.layer.cornerRadius = 15*WidthRatio;

這個(gè)時(shí)候你就會(huì)發(fā)現(xiàn)你的SearchBar完美設(shè)置了圓角

2.設(shè)置UISearchBar的placeholder的字體大小和顏色

UITextField *textfield = [searchBar valueForKey:@"_searchField"];
[textfield setValue:FontThin(14.0f*WidthRatio) forKeyPath:@"_placeholderLabel.font"];
[textfield setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

好了干貨完畢,繼續(xù)搬磚去了,喜歡的小伙伴可以點(diǎn)個(gè)贊在走呀~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容