레이블이 textToImage인 게시물을 표시합니다. 모든 게시물 표시
레이블이 textToImage인 게시물을 표시합니다. 모든 게시물 표시

2016년 1월 23일 토요일

네비게이션 바에 컬러 아이콘으로 버튼을 만들고 거기에 넘버링 까지 하려면



이러한 화면을 구성 하려면 어찌 합니까? 어떻게 할까요?

우선 네비게이션바에 바버튼 이미지는 단색으로 설정된다.

        let btImage = Helper.textToImage("1", inImage: UIImage(named: "bt_topmenu_bell_on")!, atPoint: CGPoint(x: 26, y: 21))
        
        

        let rightButton2: UIBarButtonItem = UIBarButtonItem(image: btImage.imageWithRenderingMode(.AlwaysOriginal), style: UIBarButtonItemStyle.Plain, target: self, action: "")


위 코드에서  btImage.imageWithRenderingMode(.AlwaysOriginal) 
이를 설정 해 줘야 네비게이션바에 바버튼 이미지를 원래 색상으로 처리 한다.

btImage 즉 벨 아이콘에 숫자 넘버링을 위한 코드는 helper 클래스를 만들어서 이용 하였다.

아무데서나 편하게 쓰기 위한 helper 클래스를 만들고 거기에 
text와 이미지, 그리고 포지션을 주면 이미지위에  text를 표시하는 함수를 작성 해서 이용한다.

// 이미지 위에 글 쓰기 (넘버링)
    static func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{
        
        // Setup the font specific variables
        let textColor: UIColor = UIColor.whiteColor()
        let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 9)!
        
        //Setup the image context using the passed image.
        UIGraphicsBeginImageContext(inImage.size)
        
        //Setups up the font attributes that will be later used to dictate how the text should be drawn
        let textFontAttributes = [
            NSFontAttributeName: textFont,
            NSForegroundColorAttributeName: textColor,
        ]
        
        //Put the image into a rectangle as large as the original image.
        inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))
        
        // Creating a point within the space that is as bit as the image.
        let rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)
        
        //Now Draw the text into an image.
        drawText.drawInRect(rect, withAttributes: textFontAttributes)
        
        // Create a new image out of the images we have created
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        
        // End the context now that we have the image we need
        UIGraphicsEndImageContext()
        
        //And pass it back up to the caller.
        return newImage
    }