반응형
안드로이드는 페인트에 맞춤 글꼴을 설정
페인트에 텍스트를 그리고 싶습니다. 사용자 정의 글꼴 ( 예 : Helvetica )과 굵은 체로 그리는 방법 은 무엇입니까? 나는 시스템 글꼴을 사용하고 자산에서 생성하지 않는 것을 선호합니다. 감사.
"사용자 정의 글꼴"이 자산으로 제공하는 글꼴을 의미하는 경우 다음 코드가 작동합니다.
Typeface plain = Typeface.createFromAsset(assetManager, pathToFont);
Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD)
Paint paint = new Paint();
paint.setTypeface(bold);
canvas.drawText("Sample text in bold",0,0,paint);
페인트 클래스에 사용 :
Paint paint = new Paint();
paint.setTypeface(Typeface.create("Arial",Typeface.ITALIC));
글꼴에 Android의 새로운 XML 글꼴을 사용하는 경우 페인트에 사용되는 서체를 얻으려면 다음을 사용할 수 있습니다.
val customTypeface = ResourcesCompat.getFont(context, R.font.myfont)
또는 최소 Android API> = 26 인 경우
val customTypeface = resources.getFont(R.font.myfont)
그런 다음 페인트 개체에 적용하려면 :
mTextPaint.typeface = customTypeface
자세한 내용은 https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code를 확인 하세요.
이미 사용중인 글꼴이 있고 해당 글꼴의 굵은 버전을 사용하려면이 작업을 수행 할 수 있습니다.
currentPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
currentPainter.setColor(Color.WHITE);
currentPainter.setTextSize(Utils.sp2px(getResources(), 14)); // set font size
Typeface currentTypeFace = currentPainter.getTypeface();
Typeface bold = Typeface.create(currentTypeFace, Typeface.BOLD);
currentPainter.setTypeface(bold);
위의 답변을 사용했지만이 수정은 저에게 필요했습니다. 그래서 제가 언급 할 것이라고 생각했습니다.
리소스 (Kotlin)의 글꼴을 사용하려는 경우 :
val textPaint = TextPaint()
textPaint.typeface = resources.getFont(R.font.font_name)
이것은 질문과 관련이 없을 수도 있지만 이것이 제가 찾던 것입니다. 누군가도 그것을 필요로 할 것입니다.
참고 URL : https://stackoverflow.com/questions/6042977/android-set-custom-font-to-a-paint
반응형
'Programing' 카테고리의 다른 글
RESOURCE_LOCAL 또는 JTA와 같은 지속성 단위? (0) | 2020.09.16 |
---|---|
C #에서 문자열을 정수로 변환하는 방법 (0) | 2020.09.16 |
onclick으로 PHP 기능 실행 (0) | 2020.09.16 |
현재 컨텍스트에 'ViewBag'이름이 없습니다.-Visual Studio 2015 (0) | 2020.09.16 |
Python에서 매우 긴 If 문 (0) | 2020.09.16 |