UI-切圓角、透明度、取消按鈕點擊高亮效果、按鈕文字帶底線,ui-切圓

來源:互聯網
上載者:User

UI-切圓角、透明度、取消按鈕點擊高亮效果、按鈕文字帶底線,ui-切圓
一、切UIView的某個角為圓角

如果需要將UIView的4個角全部都為圓角,做法相當簡單,只需設定其Layer的cornerRadius屬性即可(項目需要使用QuartzCore架構)。而若要指定某幾個角(小於4)為圓角而別的不變時,怎麼做呢?

其實很簡單,使用UIBezierPath,設定CAShapeLayer,給UIView設定遮罩效果即可。

// 表徵圖左上、左下切圓角UIBezierPath *phoneIconPath = [UIBezierPath bezierPathWithRoundedRect:self.phoneBGView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];CAShapeLayer *phoneIconLayer = [[CAShapeLayer alloc] init];phoneIconLayer.frame = self.phoneBGView.bounds;phoneIconLayer.path = phoneIconPath.CGPath;self.phoneBGView.layer.mask = phoneIconLayer;
// 上面代碼很簡單,這裡列下枚舉值,根據即可添加即可typedef NS_OPTIONS(NSUInteger, UIRectCorner) {    UIRectCornerTopLeft     = 1 << 0,    UIRectCornerTopRight    = 1 << 1,    UIRectCornerBottomLeft  = 1 << 2,    UIRectCornerBottomRight = 1 << 3,    UIRectCornerAllCorners  = ~0UL};

 

二、改變UIView的透明度

1、如果一個View上有子控制項,這個View的alpha改變時,子控制項的透明度也會隨著改變;

2、如果這個View是UITextField,它的alpha改變時,它的placeHolder的顏色和textColor都會隨著改變;

要求:改變View透明度,怎麼使其他保持原樣?

解答:這個時候,不要再改變alpha了,會影響其他控制項的,只需要設定背景色的透明度即可。

// 通過改變背景色的透明度,不會影響其他控制項及屬性self.pswTF.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];

 

3、怎麼改變UITextField的placeHolder的顏色呢?

太簡單了,通過運行時及KVC改變:

// 文字框更換placeHolder顏色[self.userNameTF setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];

 

三、按鈕點擊的時候,會有高亮效果,怎麼取消呢?

看下面的圖,太簡單了,這裡就不再上代碼了。

1、Button的Type改為Custom;

2、不要勾選Highlighted Adjusts Image。

 

 

四、怎麼給按鈕的文字下面添加底線呢?

思路:自訂控制項,在Button的子控制項titleLabel下面劃線。然後StoryBoard中設定Button的Class為自訂類,添加畫線顏色即可。

#import <UIKit/UIKit.h>IB_DESIGNABLE@interface KTButton : UIButton// 文字下面的底線顏色@property(nonatomic,strong) IBInspectable UIColor *lineColor;@end
#import "KTButton.h"@implementation KTButton- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {            }    return self;}-(void)setColor:(UIColor *)color{    _lineColor = [color copy];    [self setNeedsDisplay];}- (void) drawRect:(CGRect)rect {    CGRect textRect = self.titleLabel.frame;    CGContextRef contextRef = UIGraphicsGetCurrentContext();        CGFloat descender = self.titleLabel.font.descender;    if([_lineColor isKindOfClass:[UIColor class]]){        CGContextSetStrokeColorWithColor(contextRef, _lineColor.CGColor);    }        CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender + 1 + 2);    CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender + 1 + 2);        CGContextClosePath(contextRef);    CGContextDrawPath(contextRef, kCGPathStroke);}@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.