objective-C UIButton 的设计如何自定义?
How custom the design of UIButton in objective-C?
我需要帮助来创建一个自定义 UIButton,我正在创建一个调整,我有一个自定义 uiview,我知道如何在上面添加一个按钮。
但我不喜欢它的蓝色和微小的库存样式,我想像我的 UIView 一样使用颜色样式和大小自定义我的按钮,但不知道如何自定义。
我想要这样的东西:Image of button style I want
自定义按钮样式可以如下实现。
Objective-C
CustomButton.h
#import <Foundation/Foundation.h>
@interface UIImage (Utils)
+ (UIImage *)imageWithSize:(CGSize)size color:(UIColor *)color;
@end
@interface CustomButton : UIButton
- (instancetype)initWithCoder:(NSCoder *)coder;
@end
CustomButton.m
#import <UIKit/UIKit.h>
#import "CustomButton.h"
@implementation UIImage (Utils)
+ (UIImage *)imageWithSize:(CGSize)size color:(UIColor *)color
{
UIGraphicsBeginImageContextWithOptions(size, true, 0.0);
[color setFill];
UIRectFill(CGRectMake(0.0, 0.0, size.width, size.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
@implementation CustomButton: UIButton
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
UIImage *bgImage = [UIImage imageWithSize:self.bounds.size color:UIColor.blackColor];
[self setBackgroundImage:bgImage forState:UIControlStateNormal];
[self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
self.layer.cornerRadius = 40.0;
self.layer.masksToBounds = YES;
self.titleLabel.font = [UIFont systemFontOfSize:18.0 weight:UIFontWeightRegular];
}
return self;
}
@end
Swift
CustomButton.swift
struct AppStyles {
struct ActionButton {
static let backgroundColor = UIColor.black
static let textColor = UIColor.white
static let font = UIFont.systemFont(ofSize: 18.0, weight: .regular)
}
}
extension UIImage {
class func imageWithSize(_ size: CGSize, color: UIColor) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
color.setFill()
UIRectFill(CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
class CustomButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let style: AppStyles.ActionButton.Type = AppStyles.ActionButton.self
let backgroundImage = UIImage.imageWithSize(self.bounds.size, color: style.backgroundColor)
self.setBackgroundImage(backgroundImage, for: UIControl.State())
self.setTitleColor(style.textColor, for: UIControl.State())
self.layer.cornerRadius = 40.0
self.layer.masksToBounds = true
if let label = self.titleLabel {
label.font = style.font
}
self.isEnabled = true
}
}
将 UIButton 拖放到情节提要中的 UIView 并将 Identity Inspector
中的 class 设为 CustomButton
并根据需要添加约束。而已。
请找到生成的屏幕截图。希望对你有帮助。
我需要帮助来创建一个自定义 UIButton,我正在创建一个调整,我有一个自定义 uiview,我知道如何在上面添加一个按钮。
但我不喜欢它的蓝色和微小的库存样式,我想像我的 UIView 一样使用颜色样式和大小自定义我的按钮,但不知道如何自定义。
我想要这样的东西:Image of button style I want
自定义按钮样式可以如下实现。
Objective-C
CustomButton.h
#import <Foundation/Foundation.h>
@interface UIImage (Utils)
+ (UIImage *)imageWithSize:(CGSize)size color:(UIColor *)color;
@end
@interface CustomButton : UIButton
- (instancetype)initWithCoder:(NSCoder *)coder;
@end
CustomButton.m
#import <UIKit/UIKit.h>
#import "CustomButton.h"
@implementation UIImage (Utils)
+ (UIImage *)imageWithSize:(CGSize)size color:(UIColor *)color
{
UIGraphicsBeginImageContextWithOptions(size, true, 0.0);
[color setFill];
UIRectFill(CGRectMake(0.0, 0.0, size.width, size.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
@implementation CustomButton: UIButton
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
UIImage *bgImage = [UIImage imageWithSize:self.bounds.size color:UIColor.blackColor];
[self setBackgroundImage:bgImage forState:UIControlStateNormal];
[self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
self.layer.cornerRadius = 40.0;
self.layer.masksToBounds = YES;
self.titleLabel.font = [UIFont systemFontOfSize:18.0 weight:UIFontWeightRegular];
}
return self;
}
@end
Swift
CustomButton.swift
struct AppStyles {
struct ActionButton {
static let backgroundColor = UIColor.black
static let textColor = UIColor.white
static let font = UIFont.systemFont(ofSize: 18.0, weight: .regular)
}
}
extension UIImage {
class func imageWithSize(_ size: CGSize, color: UIColor) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
color.setFill()
UIRectFill(CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
class CustomButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let style: AppStyles.ActionButton.Type = AppStyles.ActionButton.self
let backgroundImage = UIImage.imageWithSize(self.bounds.size, color: style.backgroundColor)
self.setBackgroundImage(backgroundImage, for: UIControl.State())
self.setTitleColor(style.textColor, for: UIControl.State())
self.layer.cornerRadius = 40.0
self.layer.masksToBounds = true
if let label = self.titleLabel {
label.font = style.font
}
self.isEnabled = true
}
}
将 UIButton 拖放到情节提要中的 UIView 并将 Identity Inspector
中的 class 设为 CustomButton
并根据需要添加约束。而已。
请找到生成的屏幕截图。希望对你有帮助。