【发布时间】:2015-07-27 07:52:17
【问题描述】:
我有一个自定义 UITextFieldClass:
import Foundation
import UIKit
class TextFieldWithImage : UITextField {
override func leftViewRectForBounds(bounds: CGRect) -> CGRect {
return CGRect(x: self.frame.height / 4, y: self.frame.height / 4, width: self.frame.height / 2, height: self.frame.height / 2)
}
func setIcon(iconCode : String){
let imageView2 = UIImageView(frame: CGRect(x: self.frame.height / 4, y: self.frame.height / 4, width: self.frame.height / 2, height: self.frame.height / 2))
imageView2.fontAwesome(fontAwesome: "f10b")
self.leftView = imageView2
self.leftViewMode = UITextFieldViewMode.Always
}
}
我在 Identity Inspector 中将其设置为自定义类。
然后我将它们作为出口绑定到我的视图控制器: https://yadi.sk/i/8KL5gkvTi79qL
问题:当我启动我的应用程序时,我的 AppDelegate 类中出现错误: https://yadi.sk/i/ytvRof2Di79zw
这有什么问题吗?请帮我。
更新: 文件 FontAwesome.swift
//
// Font-Awesome.swift
//
// Copyright (c) 2015 Hirohisa Kawasaki. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
protocol UnicodeLiteralConvertible {
func convertToUnicode() -> String
}
extension String: UnicodeLiteralConvertible {
func convertToUnicode() -> String {
let scanner = NSScanner(string: self)
var _unicode : UInt32 = 0
if scanner.scanHexInt(&_unicode) {
return String(UnicodeScalar(_unicode))
}
return self
}
}
extension Int: UnicodeLiteralConvertible {
func convertToUnicode() -> String {
return String(UnicodeScalar(self))
}
}
class FontAwesome {
struct Static {
static var token : dispatch_once_t = 0
}
class func load() {
dispatch_once(&Static.token) {
FontManager.load("FontAwesome")
}
}
class FontManager {
class func load(fontName: String) {
if (UIFont.fontNamesForFamilyName(fontName).count > 0) {
return
}
let fontData = findFontData(fontName)
registerFontData(fontData)
}
class func findFontData(fontName: String) -> NSData {
let fontURL = NSBundle.mainBundle().URLForResource(fontName, withExtension: "otf")
if let fontURL = fontURL, let data = NSData(contentsOfURL: fontURL) {
return data
}
fatalError("\(fontName).otf is not found in the main bundle resources.")
}
class func registerFontData(fontData: NSData) {
let provider = CGDataProviderCreateWithCFData(fontData)
if let font = CGFontCreateWithDataProvider(provider) {
var error: Unmanaged<CFError>?
if !CTFontManagerRegisterGraphicsFont(font, &error) {
println(error)
}
}
}
}
}
// Use Font Awesome
extension UIFont {
class func fontAwesome(#size: CGFloat) -> UIFont {
FontAwesome.load()
if let font = UIFont(name: "FontAwesome", size: size) {
return font
}
return UIFont.systemFontOfSize(size)
}
}
extension String {
static func fontAwesome(unicode fontAwesome: UnicodeLiteralConvertible) -> String {
return fontAwesome.convertToUnicode()
}
}
// UIKit
extension UIImageView {
func fontAwesome(#fontAwesome: UnicodeLiteralConvertible) {
let _image = _fontAwesome(fontAwesome: fontAwesome)
image = _image
}
private func _fontAwesome(#fontAwesome: UnicodeLiteralConvertible, color: UIColor = UIColor.blackColor()) -> UIImage {
let unicode = fontAwesome.convertToUnicode()
let font = UIFont.fontAwesome(size: frame.height)
UIGraphicsBeginImageContextWithOptions(frame.size, false, 0)
let textSize = unicode.sizeWithAttributes([NSFontAttributeName : font])
let x = (bounds.width - textSize.width)/2
let y = (bounds.height - textSize.height)/2
let textRect = CGRect(x: x, y: y, width: textSize.width, height: textSize.height)
let style = NSMutableParagraphStyle.defaultParagraphStyle()
let attributes = [
NSFontAttributeName: font,
NSForegroundColorAttributeName: color,
NSParagraphStyleAttributeName: style
]
unicode.drawInRect(textRect, withAttributes: attributes)
let image = UIGraphicsGetImageFromCurrentImageContext()
return image
}
}
extension UIButton {
func setFontAwesome(#fontAwesome: UnicodeLiteralConvertible, forState state: UIControlState) {
let title = fontAwesome.convertToUnicode()
setTitle(title, forState: state)
let font = UIFont.fontAwesome(size: frame.height - 4) // use 2point to padding
titleLabel!.font = font
}
}
public extension UIBarButtonItem {
convenience init(fontAwesome: String, target: AnyObject?, action: Selector) {
let title = String.fontAwesome(unicode: fontAwesome)
self.init(title: title, style: .Plain, target: target, action: action)
let font = UIFont.fontAwesome(size: 25)
let attributes = [NSFontAttributeName: font]
self.setTitleTextAttributes(attributes, forState: .Normal)
}
}
但如果我评论字符串imageView2.fontAwesome(fontAwesome: "f10b"),错误仍然存在。
【问题讨论】:
-
可能是任何东西,很可能是其他东西。您需要将堆栈跟踪作为文本发布。另外,暂时删除
fontAwesome,因为这是另一个无关紧要的潜在错误来源。 -
不幸的是堆栈跟踪是空的
标签: swift uitextfield