gpt4 book ai didi

ios - 将 UILabel 子类从 Objective-C 转换为 Swift

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:45 26 4
gpt4 key购买 nike

如何将以下子类代码转换为 swift 3.1.1我试图改变自己,但我遇到了太多错误

#import <UIKit/UIKit.h>

@interface myTextLabels : UILabel


@property (nonatomic) IBInspectable NSString *overrideFontName;
@property (nonatomic) IBInspectable CGFloat iphoneFontSize;
@property (nonatomic) IBInspectable CGFloat ipadFontSize;
@end

#import "myTextLabels.h"

@implementation myTextLabels

- (void)setOverrideFontName:(NSString *)overrideFontName
{
if (![_overrideFontName isEqualToString:overrideFontName])
{
_overrideFontName = overrideFontName;

self.font = self.font;
}
}

- (void)setFont:(UIFont *)font
{
NSString *overrideFontName = self.overrideFontName;
if (overrideFontName != nil)
{
font = [UIFont fontWithName:overrideFontName size:font.pointSize];
}

[super setFont:font];
}

@end

尝试了各种方法,但错误太多这是我的尝试

import Foundation
import UIKit

class myTextLabel:UILabel{

@IBInspectable var overrideFontName: String = ""
@IBInspectable var iphoneFontSize: CGFloat = 0.0
@IBInspectable var ipadFontSize: CGFloat = 0.0

func setOverrideFontName(_ overrideFontName: String) {
if !(self.overrideFontName == overrideFontName) {
self.overrideFontName = overrideFontName
font = font
}
}

func setFont(_ font: NSFont!) {
let overrideFontName: String = self.overrideFontName
if overrideFontName != nil {
font = UIFont(name: overrideFontName, size: font.pointSize)
}
super.font = font
}

}

最佳答案

试一试:

import UIKit

class myTextLabels: UILabel {
@IBInspectable private var _overrideFontName: String = ""
@IBInspectable var overrideFontName: String {
get {
return _overrideFontName
}
set(overrideFontName) {
if !(_overrideFontName == overrideFontName) {
_overrideFontName = overrideFontName
font = font
}
}
}
@IBInspectable var iphoneFontSize: CGFloat = 0.0
@IBInspectable var ipadFontSize: CGFloat = 0.0

override func setFont(_ font: NSFont!) {
let overrideFontName: String = self.overrideFontName
if overrideFontName != nil {
font = UIFont(name: overrideFontName, size: font.pointSize)
}
super.font = font
}
}

关于ios - 将 UILabel 子类从 Objective-C 转换为 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46055917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com