适用于iOS应用的有效UI样式

Zoe*_*dia 11 ui-design ios swift

我的问题很简单.在android中,我们可以将xml样式表与布局分开,以便它可以在任何地方重用,并且可以轻松编辑以进行UI设计更改.

在iOS xcode中也可以吗?如果可以如何(如果不是来自控制器)?需要图书馆?什么是好的图书馆?

谢谢您的回答.

tot*_*tiG 13

您可以使用枚举创建自己的样式.通过在枚举枚举中放置枚举,您可以得到一个很好的分组:

enum Styles {
    enum Labels {
        case Standard
        case LargeText

        func style(label: UILabel) {
            switch self {
            case .Standard:
                label.font = UIFont.systemFontOfSize(12)
            case .LargeText:
                label.font = UIFont.systemFontOfSize(18)
            }
        }
    }

    enum Buttons {
        case RedButton

        func style(button: UIButton) {
            switch self {
            case .RedButton:
                button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

Styles.Labels.Standard.style(yourLabel)
Run Code Online (Sandbox Code Playgroud)

然后,您还可以为已设置的样式进行扩展:

extension UILabel {
    func style(style: Styles.Labels) {
        style.style(self)
    }
}

extension UIButton {
    func style(style: Styles.Buttons) {
        style.style(self)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用这样的扩展:

yourLabel.style(.Standard)
yourButton.style(.RedButton)
Run Code Online (Sandbox Code Playgroud)


KDe*_*kar 1

为此,您可以使用UIView 的UICategory类。为 set bordersborder colorspassbazier-paths等创建不同的方法corner radius。这只是其中的一小部分。类别是 UIView 的,因此您可以使用 on buttonslablestextviewtextedits

UIView+category.h

@interface UIView (category)
-(void)makeToRoundEdgeWithBorder:(CGFloat )borderwidth bordecolor:(UIColor *)color;

@end
Run Code Online (Sandbox Code Playgroud)

UIView+category.m

@implementation UIView (category)

-(void)makeToRoundEdgeWithBorder:(CGFloat )borderwidth bordecolor:(UIColor *)color
{
   NSLog(@"height %f width %f",CGRectGetHeight(self.frame),CGRectGetWidth(self.frame));
    self.layer.cornerRadius=CGRectGetHeight(self.frame)/2;
    self.layer.masksToBounds=YES;
    self.layer.borderColor=[color CGColor];
    self.layer.borderWidth=borderwidth;
}

@end
Run Code Online (Sandbox Code Playgroud)

用它

[yourlable makeToRoundEdgeWithBorder:0.0f bordercolor:[UIColor clearColor] cornerRadius:8.0f];

[yourbutton makeToRoundEdgeWithBorder:0.0f bordercolor:[UIColor clearColor] cornerRadius:8.0f];

[yourTextview makeToRoundEdgeWithBorder:0.0f bordercolor:[UIColor clearColor] cornerRadius:8.0f];

[yourTextfield makeToRoundEdgeWithBorder:0.0f bordercolor:[UIColor clearColor] cornerRadius:8.0f];
Run Code Online (Sandbox Code Playgroud)

  • 只是为了避免混淆,特别是对于初学者程序员来说,类别不是类。事实上,不存在“UICategory”这样的东西。类别只是向现有类添加新功能的一种方式。 (4认同)
  • 这不是 Swift - 因为它是公认的答案,不确定 OP 要求什么,也许也提供 Swift 扩展示例。 (3认同)