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

问题描述 投票:1回答:1

我如何将以下子类代码转换为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
    }

}
ios objective-c swift
1个回答
-1
投票

试试吧:

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
}
}
© www.soinside.com 2019 - 2024. All rights reserved.