本篇将讲述,WKInterfaceLabel、WKInterfaceImage,常见控件的属性和方法。
1.WKInterfaceLabel
WKInterfaceLabel是继承于WKInterfaceObject,而WKInterfaceObject是继承于NSObject.
//初始化
- (instancetype)init;
//设置是否隐藏
- (void)setHidden:(BOOL)hidden;
//设置透明度
- (void)setAlpha:(CGFloat)alpha;
- (void)setSemanticContentAttribute:(WKInterfaceSemanticContentAttribute)semanticContentAttribute ;
//设置水平对齐
- (void)setHorizontalAlignment:(WKInterfaceObjectHorizontalAlignment)horizontalAlignment ;
//设置垂直对齐
- (void)setVerticalAlignment:
(WKInterfaceObjectVerticalAlignment)verticalAlignment ;
//设置宽度
- (void)setWidth:(CGFloat)width;
//设置高度
- (void)setHeight:(CGFloat)height;
//设置相对宽度
- (void)setRelativeWidth:(CGFloat)width withAdjustment:(CGFloat)adjustment WK_AVAILABLE_WATCHOS_ONLY(2.0);
//设置相对高度
- (void)setRelativeHeight:(CGFloat)height withAdjustment:(CGFloat)adjustment WK_AVAILABLE_WATCHOS_ONLY(2.0);
//设置合适的宽度
- (void)sizeToFitWidth ;
//设置合适的高度
- (void)sizeToFitHeight WK_AVAILABLE_WATCHOS_ONLY(2.0);
//控制器属性名,与controller's 属性名相同。(没有用过)
@property (nonatomic, readonly, copy) NSString *interfaceProperty; // same as controller's property name
2. WKInterfaceLabel
2.1 WKInterfaceLabel常见的属性和方法
@interface WKInterfaceLabel : WKInterfaceObject
//设置文本文字
- (void)setText:(nullable NSString *)text;
//设置文本颜色
- (void)setTextColor:(nullable UIColor *)color;
//设置文本属性
- (void)setAttributedText:(nullable NSAttributedString *)attributedText;
2.2 WKInterfaceLabel用户
@interface InterfaceController()
//从storyBoard中拉线 WKInterfaceLabel
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *lb;
//富文本Label
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *attributedLb;
@end
@implementation InterfaceController
//初始化,类似于UIViewController init.
- (void)awakeWithContext:(id)context
{
[super awakeWithContext:context];
#mark - 普通设置Label
//设置label 内容
[self.lb setText:@"helloIWatch"];
//设置Label字体颜色
[self.lb setTextColor:[UIColor redColor]];
#mark - 富文本设置label
//文本内容
NSString *str = @"人生若只如初见,何事悲风秋画扇。\n等闲变却故人心,却道故人心易变。\n骊山语罢清宵半,泪雨霖铃终不怨。\n何如薄幸锦衣郎,比翼连枝当日愿。";
//创建NSMutableAttributedString
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str];
//设置文本常见属性
[attrStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30.0f],NSForegroundColorAttributeName:[UIColor blueColor]} range:NSMakeRange(0, str.length)];
//加载富文本内容
self.attributedLb setAttributedText:attrStr];
}
//将要展示 ,类似于UIViewController show UI
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}
//类似于UIViewController didDisappear
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
@end
3. WKInterfaceImage
3.1 WKInterfaceImage 常见的属性和方法
@interface WKInterfaceImage : WKInterfaceObject <WKImageAnimatable>
//设置图片,此没有缓存
- (void)setImage:(nullable UIImage *)image;
//通过NSData加载图片,此方法加载图片比setImage快
- (void)setImageData:(nullable NSData *)imageData;
//通过图片名来加载图片,此有图片缓存,如果同一图片都要使用的话,建议用此方法
- (void)setImageNamed:(nullable NSString *)imageName;
//设置模板图片颜色,首先你要将图片Render 设置为template image,然后再设置如下方法,
//这样才会有作用。具体设置如下图。
- (void)setTintColor:(nullable UIColor *)tintColor;
@end
Snip20170307_13.png
3.2 WKInterfaceImage用法和区别
Note:
a.图片如果经常用的话,将它放在watch APP Assets.xcassets目录下 和用 setImageNamed: 加载图片。
b.- (void)setImageData:(nullable NSData *)imageData方法,是存储从网络下载的图片,图片会放在WatchKit extension’s 目录下。
c.- (void)setImageNamed:(nullable NSString *)imageName,是设置动图,此方法不会存储图片。
d.如果经常用到图片的话,可以将图片先存储起来,用WKInterfaceDevice 类,但是此方法可以在watchOS 1用,在watchOS 2行不通;
e. 更多的image内容,请看官方文档。
@interface InterfaceController()
//用名字加载图片,会存储
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceImage *topImageView;
//用UIImage加载图片,不会存储图片
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceImage *middleImageView;
@end
@implementation InterfaceController
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
//图片要放在watch APP Assets.xcassets文件夹下。
[self.middleImageView setImageNamed:@"Personal communication"];
//此方法加载图片,不会存储图片
UIImage *image = [UIImage imageNamed:@"Lightweight interactions"];
[self.middleImageView setImage:image];
// Configure interface objects here.
}
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
@end
以上是WKInterfaceLabel和WKInterfaceImage属性和方法的用法,有不足的地方,请指出,感谢!!
网友评论