2017-09-05 37 views
1

我怎麼以下子類代碼轉換成快捷3.1.1 我一直在努力,我自己轉換,但我得到了太多的錯誤從Objective-C的轉換的UILabel子類斯威夫特

#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 
    } 

} 
+0

瓦你有錯誤嗎? – Fogmeister

+0

使用Objective-C選擇器'setOverrideFontName'的方法'setOverrideFontName'與具有相同Objective-C選擇器的'overrideFontName'的setter衝突 – Superllanboy

回答

-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 
} 
} 
+0

已嘗試過您的代碼,我現在有更多錯誤如下 – Superllanboy

+0

使用未聲明類型'NSFont' – Superllanboy

+0

將'String'類型的非可選值與nil比較總是返回true – Superllanboy