Programing

iPhone UILabel 텍스트 부드러운 그림자

lottogame 2020. 12. 6. 20:49
반응형

iPhone UILabel 텍스트 부드러운 그림자


iPhone의 UILabel에서 부드러운 그림자가 지원되지 않는다는 것을 알고 있습니다. 그렇다면 내 자신을 구현하는 가장 좋은 방법은 무엇입니까?

편집하다:

분명히 나는 ​​하위 클래스 UILabel를 만들고 -drawRect에서 그릴 것입니다. 제 질문은 라벨의 내용을 그래픽으로 가져 와서 그 주위에 그리고 흐리게 처리하는 방법입니다.

편집 2 :

나는 약 1 년 후에이 질문으로 돌아왔다. 그 동안 레이블에 부드러운 그림자를 쉽게 추가하고 반경 등을 조정하고 텍스트 자체에 그라디언트를 그릴 수있는 클래스를 만들었습니다. GitHub에서 찾을 수 있습니다 : https://github.com/doukasd/iOS-Components/tree/master/Views


이 답변이 비슷한 질문은 UILabel의 뒤에 흐리게 그림자를 그리는 코드를 제공합니다. 작성자는 CGContextSetShadow ()를 사용하여 그려진 텍스트의 그림자를 생성합니다.


3.2부터는 SDK에서 그림자를 직접 지원합니다.

label.layer.shadowColor = [label.textColor CGColor];
label.layer.shadowOffset = CGSizeMake(0.0, 0.0);

<QuartzCore/QuartzCore.h>일부 매개 변수를 가져 와서 재생 :

label.layer.shadowRadius = 3.0;
label.layer.shadowOpacity = 0.5;

그리고 라벨 경계에 의해 잘린 그림자를 찾으면 :

label.layer.masksToBounds = NO;

마침내 설정

label.layer.shouldRasterize = YES

UILabel의 shadowColor 및 shadowOffset 속성을 사용하는 것이 좋습니다.

UILabel* label = [[UILabel alloc] init];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0,1);

IIDan의 답변에 추가로 : 어떤 목적을 위해 설정해야합니다.

label.layer.shouldRasterize = YES

그림자를 렌더링하는 데 사용되는 블렌드 모드 때문이라고 생각합니다. 예를 들어, 어두운 배경과 흰색 텍스트가 있었고 검은 그림자 광선을 사용하여 텍스트를 "강조 표시"하고 싶었습니다. 이 속성을 설정할 때까지 작동하지 않았습니다.


다음 과 같이 뷰의 레이어 에 (부드러운) 그림자를 적용합니다 .

UILabel *label = [[UIabel alloc] init];
label.layer.shadowColor = [[UIColor whiteColor] CGColor];
label.layer.shadowOpacity = 1.0;

최신 상태로 유지하려면 : Swift에서 그림자를 만드는 것은 다음과 같이 쉽습니다.

QuartzCore 프레임 워크 가져 오기

import QuartzCore

그리고 그림자 속성을 레이블에 설정하십시오.

titleLabel.shadowColor = UIColor.blackColor()
titleLabel.shadowOffset = CGSizeMake(0.0, 0.0)
titleLabel.layer.shadowRadius = 5.0
titleLabel.layer.shadowOpacity = 0.8
titleLabel.layer.masksToBounds = false
titleLabel.layer.shouldRasterize = true

_nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_nameLabel.font = [UIFont boldSystemFontOfSize:19.0f];
_nameLabel.textColor = [UIColor whiteColor];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.2];
_nameLabel.shadowOffset = CGSizeMake(0, 1);

알파 값을 설정하려면 [UIColor colorWithWhite : 0 alpha : 0.2]를 사용해야한다고 생각합니다.


나는 거의 모든 기술 (FXLabel 제외)을 시도했지만 iOS 7에서 작동 할 수 없었습니다. 결국 저에게 완벽하게 작동하는 THLabel을 찾았습니다. 저는 Interface Builder에서 THLabel을 사용하고 사용자 정의 런타임 속성을 설정하여 프로그래머가 아닌 사람이 모양과 느낌을 쉽게 제어 할 수 있도록했습니다.

https://github.com/MuscleRumble/THLabel


이것은 속임수처럼

UILabel *customLabel = [[UILabel alloc] init];

UIColor *color = [UIColor blueColor];
customLabel.layer.shadowColor = [color CGColor];
customLabel.layer.shadowRadius = 5.0f;
customLabel.layer.shadowOpacity = 1;
customLabel.layer.shadowOffset = CGSizeZero;
customLabel.layer.masksToBounds = NO;

소프트 섀도우 지원 및 기타 여러 효과가있는 UILabel 하위 클래스를 제공하는 라이브러리를 작성했습니다.

https://github.com/nicklockwood/FXLabel


Subclass UILabel, as stated, then, in drawRect:, do [self drawTextInRect:rect]; to get the text drawn into the current context. Once it is in there, you can start working with it by adding filters and whatnot. If you want to make a drop shadow with what you just drew into the context, you should be able to use:

CGContextSetShadowWithColor()

Look that function up in the docs to learn how to use it.


As of iOS 5 Apple provides a private api method to create labels with soft shadows. The labels are very fast: I'm using dozens at the same time in a series of transparent views and there is no slowdown in scrolling animation.

This is only useful for non-App Store apps (obviously) and you need the header file.

$SBBulletinBlurredShadowLabel = NSClassFromString("SBBulletinBlurredShadowLabel");

CGRect frame = CGRectZero;

SBBulletinBlurredShadowLabel *label = [[[$SBBulletinBlurredShadowLabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:12];
label.text = @"I am a label with a soft shadow!";
[label sizeToFit];

In Swift 3, you can create an extension:

import UIKit

extension UILabel {
    func shadow() {
        self.layer.shadowColor = self.textColor.cgColor
        self.layer.shadowOffset = CGSize.zero
        self.layer.shadowRadius = 3.0
        self.layer.shadowOpacity = 0.5
        self.layer.masksToBounds = false
        self.layer.shouldRasterize = true
    }
}

and use it via:

label.shadow()

Subclass UILabel, and override -drawInRect:

참고URL : https://stackoverflow.com/questions/1709131/iphone-uilabel-text-soft-shadow

반응형